//
//  NotesCollectionViewCell.m
//  TravelFirst
//
//  Created by lanou on 15/9/19.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "NotesCollectionViewCell.h"

@implementation NotesCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        UIView *backView = [[UIView alloc] init];
        backView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:backView];
        
        self.pictureImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - 30)];
        self.pictureImageView.backgroundColor = [UIColor blueColor];
        self.pictureImageView.layer.masksToBounds = YES;
        self.pictureImageView.layer.cornerRadius = 8;
        [backView addSubview:self.pictureImageView];
        
        self.avatarButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        self.avatarButton.frame = CGRectMake(frame.size.width - 45, frame.size.height - 45, 35, 35);
        self.avatarButton.layer.masksToBounds = YES;
        self.avatarButton.layer.cornerRadius = self.avatarButton.width / 2;
        self.avatarButton.backgroundColor = [UIColor greenColor];
        [backView addSubview:self.avatarButton];
        
        self.pictureLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.pictureImageView.height - 15 - 30, self.pictureImageView.width - 10 - self.avatarButton.width - 15, 30)];
        //self.pictureLabel.backgroundColor = [UIColor brownColor];
        self.pictureLabel.textColor = [UIColor whiteColor];
        self.pictureLabel.font = [UIFont boldSystemFontOfSize:17];
        self.pictureLabel.userInteractionEnabled = YES;
        [backView addSubview:self.pictureLabel];
        
        self.messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, frame.size.height - 30, frame.size.width - 10 - self.avatarButton.width - 15, 30)];
        self.messageLabel.font = [UIFont boldSystemFontOfSize:13];
        self.messageLabel.textColor = [UIColor grayColor];
        [backView addSubview:self.messageLabel];
        
        self.viewImage = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width - 120, 20, 15, 15)];
        self.viewImage.image = [UIImage imageNamed:@"icon_view_white_14x14@2x"];
        //self.viewImage.backgroundColor = [UIColor purpleColor];
        self.viewImage.userInteractionEnabled = YES;
        [self.pictureImageView addSubview:self.viewImage];
        
        self.viewLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.viewImage.right + 2, self.viewImage.top + 2, 25, 15)];
        self.viewLabel.font = [UIFont boldSystemFontOfSize:10];
        self.viewLabel.textColor = [UIColor whiteColor];
        //self.viewLabel.textAlignment = NSTextAlignmentCenter;
        //self.viewLabel.backgroundColor = [UIColor redColor];
        self.viewLabel.userInteractionEnabled = YES;
        [self.pictureImageView addSubview:self.viewLabel];
        
        self.favourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.viewLabel.right, 20, 15, 15)];
        self.favourImageView.image = [UIImage imageNamed:@"icon_actionbar_great1_20x20@2x"];
        //self.favourImageView.backgroundColor = [UIColor purpleColor];
        self.favourImageView.userInteractionEnabled = YES;
        [self.pictureImageView addSubview:self.favourImageView];
        
        self.favourLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.favourImageView.right + 2, 22, 20, 15)];
        self.favourLabel.font = [UIFont boldSystemFontOfSize:10];
        self.favourLabel.textColor = [UIColor whiteColor];
        //self.favourLabel.backgroundColor = [UIColor redColor];
        self.favourLabel.userInteractionEnabled = YES;
        [self.pictureImageView addSubview:self.favourLabel];
        
        self.commentImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.favourLabel.right + 2, 20, 15, 15)];
        //self.commentImageView.backgroundColor = [UIColor purpleColor];
        self.commentImageView.image = [UIImage imageNamed:@"icon_comment_13x13@2x"];
        self.commentImageView.userInteractionEnabled = YES;
        [self.pictureImageView addSubview:self.commentImageView];
        
        self.commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.commentImageView.right + 2, 20, 20, 15)];
        self.commentLabel.font = [UIFont systemFontOfSize:10];
        self.commentLabel.textColor = [UIColor whiteColor];
        //self.commentLabel.backgroundColor = [UIColor redColor];
        self.commentLabel.userInteractionEnabled = YES;
        [self.pictureImageView addSubview:self.commentLabel];
        backView.frame = CGRectMake(0, 0, frame.size.width, self.messageLabel.bottom);
        backView.layer.masksToBounds = YES;
        backView.layer.cornerRadius = 8;
    }
    return self;
}


- (void)setValueWithModel:(NotesModel *)model target:(id)target action:(SEL)action tag:(NSInteger)tag
{
    [self.pictureImageView sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:nil];
    [self.avatarButton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.avatar] forState:(UIControlStateNormal) placeholderImage:nil];
    self.pictureLabel.text = model.name;
    
    //NSString *dateStr = [self dateTransformWith:model.created_date];
    self.messageLabel.text = [NSString stringWithFormat:@"%@ · %@天%@图 · by %@", model.start_date, model.total_days, model.photo_number, model.nickname];
    
    self.viewLabel.text = [NSString stringWithFormat:@"%@", model.view_count];
    [self.viewLabel sizeToFit];
    self.favourLabel.text = [NSString stringWithFormat:@"%@", model.home_fav_count];
    [self.favourLabel sizeToFit];
    self.commentLabel.text = model.comment_count;
    [self.favourLabel sizeToFit];
    
    [self.avatarButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    self.avatarButton.tag = 1000 + tag;
}

// 时间戳转时间
- (NSString *)dateTransformWith:(NSString *)dateStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dateStr floatValue] + 28800];
    
    NSString *dateString = [formatter stringFromDate:date];
    NSString *string = [dateString substringToIndex:9];
    
    return string;
}

@end
